package com.idc.resource.manager;

import com.idc.resource.dal.dto.TCompanyFile;
import com.idc.resource.dal.dto.TCompanyInfo;
import com.idc.resource.dal.dto.TUserInfo;
import com.idc.resource.dal.mapper.TUserInfoMapper;
import com.idc.resource.enums.ErrorCodeEnum;
import com.idc.resource.enums.ExitsFlagEnum;
import com.idc.resource.enums.UserLevelEnum;
import com.idc.resource.exception.IdcServiceException;
import com.idc.resource.model.UserRegisterReqDto;
import com.idc.resource.model.UserInfoResDto;
import com.idc.resource.util.BeanMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户管理(删除、创建、修改用户)
 * Created by Lijing on 2017/7/4.
 */
@Slf4j
@Component
public class UserManager {
    /** 用户数据库操作 */
    @Autowired
    private TUserInfoMapper tUserInfoMapper;
    /** 企业信息管理 */
    @Autowired
    private CompanyManager companyManager;

    /**
     * 根据用户名和密码查询用户信息
     * @param userId 用户名
     * @param loginPwd 登录密码
     * @return 用户信息
     */
    public TUserInfo selectOne(String userId,String loginPwd){
        TUserInfo selectInfo = new TUserInfo();
        if(StringUtils.isBlank(userId)){
            return null;
        }
        selectInfo.setUserId(userId);
        selectInfo.setLoginPwd(loginPwd);
        try {
            List<TUserInfo> list = tUserInfoMapper.selective(selectInfo);
            if(list==null||list.isEmpty()){
                return null;
            }
            if(list.size()>1){
                log.info("根据用户名[{}]查到{}条用户记录，数据异常",userId,list.size());
                throw new IdcServiceException(ErrorCodeEnum.DATA_ERROR.getCode(),ErrorCodeEnum.DATA_ERROR.getDesc());
            }
            return list.get(0);
        }catch (IdcServiceException ise){
            throw ise;
        }catch (Exception e){
            log.info("查询用户信息数据库异常，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
    }

    /**
     * 根据条件查询用户信息
     * @param userInfo 查询条件
     * @return 用户信息
     */
    public List<TUserInfo> selectByParam(TUserInfo userInfo){
        try {
            return tUserInfoMapper.selective(userInfo);
        }catch (Exception e){
            log.error("查询用户信息数据库操作异常，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
    }

    /**
     * 更新用户信息
     * @param tUserInfo 用户信息
     * @return 更新结果
     */
    public int updateUser(TUserInfo tUserInfo){
        try {
            return tUserInfoMapper.updateByPrimaryKeySelective(tUserInfo);
        }catch (Exception e){
            log.error("更新用户信息数据库操作异常，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
    }

    /**
     * 新增用户信息
     * @param tUserInfo 用户信息
     * @return 新增结果
     */
    public int addUser(TUserInfo tUserInfo){
        try {
            tUserInfo.setCreateTime(new Date());
            return tUserInfoMapper.insertSelective(tUserInfo);
        }catch (Exception e){
            log.error("更新用户信息数据库操作异常，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
    }

    /**
     * 用户注册
     * @param userInfoReqDto 用户注册请求
     * @return 用户注册响应
     */
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public UserInfoResDto register(UserRegisterReqDto userInfoReqDto){
        TUserInfo userInfo = BeanMapperUtil.objConvert(userInfoReqDto,TUserInfo.class);
        TCompanyInfo tCompanyInfo = BeanMapperUtil.objConvert(userInfoReqDto,TCompanyInfo.class);
        TCompanyFile tCompanyFile = BeanMapperUtil.objConvert(userInfoReqDto,TCompanyFile.class);
        tCompanyInfo.setBusiCer(ExitsFlagEnum.EXITS.getCode());
        tCompanyInfo.setOrgCer(ExitsFlagEnum.EXITS.getCode());
        tCompanyInfo.setTaxCer(ExitsFlagEnum.EXITS.getCode());
        tCompanyInfo = companyManager.registerCompanyInfo(tCompanyInfo,tCompanyFile);
        try {
            userInfo.setCompanyId(tCompanyInfo.getId());
            userInfo.setLoginStatus("0");
            userInfo.setUserLv(UserLevelEnum.MANAGER.getCode());
            userInfo.setCreateTime(new Date());
            userInfo.setLoginStatus("0");
            userInfo.setCreatedBy("system-register");
            tUserInfoMapper.insertSelective(userInfo);
            return BeanMapperUtil.objConvert(userInfo,UserInfoResDto.class);
        }catch (Exception e){
            log.error("更新用户信息数据库操作异常，原因:",e);
            throw new IdcServiceException(ErrorCodeEnum.DB_ERROR.getCode(),ErrorCodeEnum.DB_ERROR.getDesc());
        }
    }

    /**
     * 根据id查询用户信息
     * @param id id
     * @return 用户信息
     */
    public TUserInfo selectById(Long id){
        return tUserInfoMapper.selectByPrimaryKey(id);
    }

}
